﻿using PostSharp.Extensibility;
using PostSharp.Laos;
using PostSharp.Laos.Weaver;
using XpoOneWayMultiAssociations;

namespace XpoOneWayMultiAssociationWeaver
{
   /// <summary>
   /// returns the correct weaver, always the edm weaver
   /// </summary>
   public class XpoOneWayMultiAssociationWeaverFactory : Task, ILaosAspectWeaverFactory
   {
      #region Methods

      /// <summary>
      /// creates the weaver
      /// </summary>
      /// <param name="aspect">the EDMSubAspect that instantiated this factory</param>
      /// <returns>
      /// the weaver that will do the lowlever hardcore work
      /// </returns>
      public LaosAspectWeaver CreateAspectWeaver(ILaosAspect aspect)
      {
         if (aspect is OtherEndFieldSubAspect)
         {
            var oneWayAssociation = (OtherEndFieldSubAspect)aspect;
            return new OtherEndFieldWeaver(oneWayAssociation.FieldTypeName, oneWayAssociation.FieldName,
                                                            oneWayAssociation.AssociationName);
         }
         if (aspect is AssociationAttributeSubAspect)
         {
            var addAssociationAttribute = (AssociationAttributeSubAspect) aspect;
            return new AssociationAttributeWeaver(addAssociationAttribute.PropertyName,
                                                     addAssociationAttribute.AssociationName);
         }

         return null;
      }

      #endregion
   }
}